package aad.storyboard.view;

import java.util.ArrayList;

import utils.ViewUtils;


import aad.storyboard.controller.DragDropManager;
import aad.storyboard.model.Circle;
import aad.storyboard.model.DropSpot;
import aad.storyboard.model.GraphicalObject;
import aad.storyboard.model.Image;
import aad.storyboard.model.Layer;
import aad.storyboard.model.ParticleEffect;
import aad.storyboard.model.Scene;
import aad.storyboard.model.Script;
import aad.storyboard.model.Story;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class GraphicView extends ImageView{
        
        private Scene currentScene;
        private ArrayList<Image> images;
        private ArrayList<Script> scripts;
        private ArrayList<Layer> layers;
        private Matrix idMatrix;

        //Drag&Drop
        private ArrayList<DropSpot> dropspots;  
        private Boolean isDragging=false;       
        private Image draggedImage;
        
        //Fading
        private Bitmap fadeTarget;
        private Paint fadePaint;
        private Bitmap blurredImage;
        private Image clickableImage;
        private Paint blurPaint;

        //Particles
        private ParticleEffect explosion;
        
        //Blinking
        private ArrayList <GraphicalObject> blinkObjects;
        
                
        public GraphicView(Context context, AttributeSet attrs){
        super(context, attrs);
        this.setBackgroundColor(Color.WHITE);
        this.idMatrix = new Matrix();
        ViewUtils.displayDensity = context.getResources().getDisplayMetrics().densityDpi;
        ViewUtils.screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        ViewUtils.screenHeight = context.getResources().getDisplayMetrics().heightPixels;               
    }
        
        public GraphicView(Context context){
        super(context);   
        this.setBackgroundColor(Color.WHITE);     
        ViewUtils.displayDensity = context.getResources().getDisplayMetrics().densityDpi;
        ViewUtils.screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        ViewUtils.screenHeight = context.getResources().getDisplayMetrics().heightPixels;               
        
    }
        
        @Override
        public void onDraw(Canvas canvas){
                canvas.setMatrix(idMatrix);
        currentScene = Story.getInstance().getCurrentChapter().getCurrentScene();
        layers = currentScene.getLayerMap();        
        
        for(int i = 0;i<layers.size();i++){
                images = layers.get(i).getImages();   
                
                for(Image img : images){
                        //Log.e("Tag","drawing image :"+img.getBitmap().getHeight()+","+img.getBitmap().getWidth()+"  onto "+canvas.getHeight()+","+canvas.getWidth());
                        canvas.drawBitmap(img.getBitmap(), img.getPosX(),img.getPosY(), img.getPaint());
                        if (img.getBlurBitmap()!=null)
                        {
                                canvas.drawBitmap(img.getBlurBitmap(), img.getPosX(), img.getPosY(), img.getBlurPaint());
                        }
           }
        }
        
        drawDropSpots(canvas);        

        drawFading(canvas);
        
        drawParticles(canvas);  
        
        drawBlinkObjects(canvas);
       
        try {
                        Thread.sleep(50);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        //this.invalidate();
        
}
        
        private void drawParticles(Canvas canvas) {
                if (explosion != null) {
                        explosion.draw(canvas);
                }               
        }

        public void setParticleEffect(ParticleEffect explosion)
        {
                this.explosion=explosion;
        }
        
        private void drawDropSpots(Canvas canvas) {
                dropspots=DragDropManager.getInstance().getDropSpots();
        for (DropSpot d: dropspots)
        {       if (draggedImage!=null)
                {                       
                 canvas.drawCircle( d.getPosX(), d.getPosY(), d.getRadius(), d.getPaint());
                 
                }
        }
                
        }

        /*@Override
    public boolean onTouchEvent(MotionEvent event) {
                if(isDragging){
                
                int eventaction = event.getAction();
                        
        int X = (int)event.getX(); 
        int Y = (int)event.getY(); 
        
        DragDropManager.getInstance().handleDragDrop(eventaction,X, Y);
        
        return true;
                }
                return false;
    }*/

        public void setDraggedImage(Image img) {
                this.draggedImage=img;
                
        }
        
        public Image getDraggedImage()
        {
                return draggedImage;
        }

        public void setDragging(boolean b) {
                isDragging=b;
                
        }
    

        public ArrayList<Image> getImages() {
                
                return images;
        }

        public void drawFading (Canvas canvas)
        {
                if (fadeTarget!=null&&fadePaint!=null)
                {
                        canvas.drawBitmap(fadeTarget, 0, 0, fadePaint);         
                } 
        }
        
        public void setFadeTarget(Bitmap target, Paint p) {
                
                this.fadeTarget=target;
                fadePaint=p;
        }

        public void setBlurredImage(Bitmap alpha, Image image, Paint paint) {
                this.blurredImage=alpha;
                this.clickableImage=image;
                this.blurPaint=paint;
                
        }

        public void setBlinkObjects(ArrayList<GraphicalObject> objects) {
                this.blinkObjects=objects;
        }
    
        public void drawBlinkObjects(Canvas canvas)
        {
                if (blinkObjects!=null)
                {                       
                        for (GraphicalObject g: blinkObjects)
                                ((Circle)g).draw(canvas);       
                } 
        }

        }
